package com.china08.yxyapi.api.manage.tec.teaching.zujuan.previewpaper;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.paper.Paper;
import com.china08.yxyapi.entity.mg.paper.PaperQues;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.PaperQuesRepository;
import com.china08.yxyapi.repository.mg.PaperRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 组卷编辑-加载存储
 * 
 * 
 */
@RestController
@CrossOrigin
public class ZujuanPaperLoadStorageController extends YxyApiBaseController {

    @Autowired
    private PaperRepository paperRepository;

    @Autowired
    private PaperQuesRepository paperQuesRepository;

    @ApiOperation(value = "组卷编辑-加载存储")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/zujuan/paper/{paperId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response,
            @PathVariable(value = "paperId") String paperId) {
        RespModel respModel = new RespModel();
        Paper paper = paperRepository.findOne(paperId);
        List<String> checkList = new ArrayList<String>();
        // 是否显示主标题
        if (paper.getTitleFlag() != null && paper.getTitleFlag() == 1) {
            checkList.add("1");
        }
        respModel.title1 = paper.getTitle();
        // 是否显示副标题
        if (paper.getSubTitleFlag() != null && paper.getSubTitleFlag() == 1) {
            checkList.add("2");
            respModel.title2 = paper.getSubTitle();
        }
        // 装订线
        if (paper.getBindingFlag() != null && paper.getBindingFlag() == 1) {
            checkList.add("3");
        }
        // 保密标记
        if (paper.getSecretLineFlag() != null && paper.getSecretLineFlag() == 1) {
            checkList.add("4");
        }
        // 试卷信息栏
        if (paper.getPaperInfoFlag() != null && paper.getPaperInfoFlag() == 1) {
            checkList.add("5");
            respModel.title3 = paper.getPaperInfo();
        }
        // 是否显示考生输入栏
        if (paper.getStudentColumnFlag() != null && paper.getStudentColumnFlag() == 1) {
            checkList.add("6");
            respModel.title4 = paper.getStudentColumn();
        }
        // 誉分栏
        if (paper.getScoreColumnFlag() != null && paper.getScoreColumnFlag() == 1) {
            checkList.add("7");
        }
        // 注意事项
        if (paper.getAttentionFlag() != null && paper.getAttentionFlag() == 1) {
            checkList.add("8");
        }
        // 分卷及注释
        if (paper.getPaperAnnotationFlag() != null && paper.getPaperAnnotationFlag() == 1) {
            checkList.add("9");
            if (paper.getPaperAnnotation() != null && paper.getPaperAnnotation().size() > 0) {
                respModel.title5 = paper.getPaperAnnotation().get(1) != null ? paper.getPaperAnnotation().get(1) : "";
                respModel.title6 = paper.getPaperAnnotation().get(2) != null ? paper.getPaperAnnotation().get(2) : "";
            }
        }
        // 大题及注释
        if (paper.getQuestionAnnotationFlag() != null && paper.getQuestionAnnotationFlag() == 1) {
            checkList.add("10");
            if (paper.getQuestionAnnotation() != null && paper.getPaperAnnotation().size() > 0) {
                respModel.title7 = paper.getQuestionAnnotation().get(1) != null ? paper.getQuestionAnnotation().get(1)
                        : "";
                respModel.title8 = paper.getQuestionAnnotation().get(2) != null ? paper.getQuestionAnnotation().get(2)
                        : "";
                respModel.title9 = paper.getQuestionAnnotation().get(3) != null ? paper.getQuestionAnnotation().get(3)
                        : "";
                respModel.title10 = paper.getQuestionAnnotation().get(4) != null ? paper.getQuestionAnnotation().get(4)
                        : "";
            }
        }
        // 大题评分区
        if (paper.getQuestionScoreFlag() != null && paper.getQuestionScoreFlag() == 1) {
            checkList.add("11");
        }
        respModel.checkList = checkList;
        // 学段,学科
        respModel.stage = paper.getPhaseId();
        respModel.stageName = paper.getPhaseName();
        respModel.subject = paper.getSubjectId();
        respModel.subjectName = paper.getSubjectName();
        // 所有试题list
        QuestionRespModel questionRespModel = null;
        List<QuestionRespModel> listModels = new ArrayList<QuestionRespModel>();
        List<PaperQues> list = paperQuesRepository.findByPaperId(paperId);

        Integer danxuan = 0;
        Integer duoxuan = 0;
        Integer tiankong = 0;
        Integer jieda = 0;
        if (CollectionUtils.isNotEmpty(list)) {
            for (PaperQues paperQues : list) {
                String quesType = paperQues.getQuesType();
                if (StringUtils.equals("1", quesType)) {
                    danxuan = danxuan + 1;
                } else if (StringUtils.equals("2", quesType)) {
                    tiankong = tiankong + 1;
                } else if (StringUtils.equals("8", quesType)) {
                    duoxuan = duoxuan + 1;
                } else {
                    jieda = jieda + 1;
                }
                // 题的模型
                questionRespModel = new QuestionRespModel();
                questionRespModel.quesId = paperQues.getQuesId();
                questionRespModel.quesType = paperQues.getQuesType();
                questionRespModel.know = paperQues.getKnowId();
                questionRespModel.point = paperQues.getScore();
                listModels.add(questionRespModel);
            }
            respModel.quesIdList = listModels;
        }
        respModel.danxuan = danxuan;
        respModel.duoxuan = duoxuan;
        respModel.tiankong = tiankong;
        respModel.jieda = jieda;
        respModel.paperId = paperId;
        return respModel;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 试卷id
        public String paperId;
        // 学段
        public String stage;
        // 学段名称
        public String stageName;
        // 学科
        public String subject;
        // 学科名称
        public String subjectName;
        // 主标题
        public String title1;
        // 副标题
        public String title2;
        // 试卷信息栏
        public String title3;
        // 考生输入栏
        public String title4;
        // 第1卷说明
        public String title5;
        // 第2卷说明
        public String title6;
        // 单选题注释及分数
        public String title7;
        // 多选题注释及分数
        public String title8;
        // 填空题注释及分数
        public String title9;
        // 解答题注释及分数
        public String title10;
        // 给个集合装基本的配置
        public List<String> checkList;
        // 题的存储
        public List<QuestionRespModel> quesIdList;
        // 单选数量
        public Integer danxuan;
        // 多选数量
        public Integer duoxuan;
        // 填空数量
        public Integer tiankong;
        // 解答数量
        public Integer jieda;
    }

    protected static class QuestionRespModel {
        // 试题id
        public String quesId;
        // 知识点集合
        public List<Integer> know;
        // 题的分数
        public Integer point;
        // 试题类型
        public String quesType;
    }
}
